home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 1 / Cream of the Crop 1.iso / PROGRAM / SNIP9_91.ARJ / MOUSE1.C < prev    next >
C/C++ Source or Header  |  1990-08-25  |  3KB  |  95 lines

  1. /*
  2. **  MOUSE.C by Doug Boone
  3. */
  4.  
  5. #include <dos.h>
  6. #include <stdio.h>
  7. #include <conio.h>
  8.  
  9. int     init_mouse(void);
  10. void    button_release(int);
  11. int     button_press(void);
  12.  
  13. /* initialize the mouse, return 0 for no mouse */
  14. /* or the number of buttons on mouse           */
  15.  
  16. int init_mouse(void)   /* initialize the mouse driver */
  17. {
  18.         union REGS inregs;
  19.  
  20.         inregs.x.ax = 0;
  21.         int86(0x33,&inregs,&inregs);
  22.         if (inregs.x.ax)
  23.                 return(inregs.x.bx);
  24.         return(0);
  25. }
  26.  
  27. /* Wait for the mouse to be released. Not many people can release */
  28. /* buttons quickly enough for their computer so you have to wait  */
  29.  
  30. void button_release(int which)
  31. {
  32.         union REGS inregs;
  33.  
  34.         do
  35.         {       inregs.x.ax = 6;
  36.                 inregs.x.bx = 1 << which;
  37.                 int86(0x33,&inregs,&inregs);
  38.         } while (inregs.x.ax & (1 << which));
  39. }
  40.  
  41. int button_press(void)
  42. {
  43.         union REGS inregs;
  44.  
  45.         inregs.x.ax = 3;    /* you can also get the mouse position... */
  46.         int86(0x33,&inregs,&inregs);
  47.  
  48.         if (inregs.x.bx == 0)                   /* no buttons pressed */
  49.                 return(0);
  50.  
  51.         if (inregs.x.bx & 0x0001)               /* left button pushed */
  52.         {       button_release(0);
  53.                 return(1);
  54.         }
  55.  
  56.         else if (inregs.x.bx & 0x0002)         /* right button pushed */
  57.         {       button_release(1);
  58.                 return(2);
  59.         }
  60.         else if (inregs.x.bx & 0x0003)        /* center button pushed */
  61.         {       button_release(2);
  62.                 return(1);
  63.         }
  64.         else    return(-1);                  /* an error has occured? */
  65. }
  66.  
  67. int main(void)
  68. {
  69.         int  buttons;
  70.  
  71.         if ((buttons = init_mouse()) > 0)
  72.                 printf("\n Your mouse has %d buttons on it. \n",buttons);
  73.         else
  74.         {       printf("\n\a You don't have a mouse, or it isn't ");
  75.                 printf("installed. Fix that before running again.\n");
  76.                 exit(1);
  77.         }
  78.  
  79.         printf("\n Press any key to exit \n");
  80.         while (!kbhit())  /* This is the only statement that may not  */
  81.                           /* work on any compiler. kbhit() isn't ANSI */
  82.                           /* but most compilers support it.           */
  83.         {
  84.                 switch(button_press())
  85.                 {
  86.                 case 1:    cputs("\n\rLeft button pressed"); break;
  87.                 case 2:    cputs("\n\rRight button pressed"); break;
  88.                 case 3:    cputs("\n\rCenter button pressed"); break;
  89.                 case -1:   cputs("\n\rMouse error reported"); break;
  90.                 }
  91.         }
  92.         cputs("\n\r\n\r Done!\n\r");
  93.         exit(0);
  94. }
  95.